﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.BLL.DicMgr;
using Lenovo.CFI.BLL.Sys;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.Common.Sys;
using Lenovo.CFI.Common.Dic;

namespace Lenovo.CFI.Web.VP
{
    public partial class Stat2 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                for (int i = 2012; i < 2020; i++)
                {
                    this.DdlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                this.DdlYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;

                this.GvList.Visible = false;

                this.Page.Form.DefaultButton = this.BtnStat.UniqueID;
            }
        }


        object[] all = null;
        DateTime from;
        DateTime to;

        protected void BtnStat_Click(object sender, EventArgs e)
        {
            int year = Convert.ToInt32(this.DdlYear.SelectedValue);

            this.from = new DateTime(year, 1, 1);
            this.to = new DateTime(year, 12, 31);

            Dictionary<int, object[]> monthNumbers = new StatBl().TypeNumberByTime(from , to);
            this.all = monthNumbers[0];
            monthNumbers.Remove(0);

            this.GvList.Visible = true;
            this.GvList.DataSource = monthNumbers;
            this.GvList.DataBind();
        }



        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)    // 数据行
            {
                KeyValuePair<int, object[]> number = (KeyValuePair<int, object[]>)e.Row.DataItem;

                string monthF = this.from.AddMonths(number.Key - 1).ToString("yyyy-MM-dd");
                string monthT = this.from.AddMonths(number.Key).AddDays(-1).ToString("yyyy-MM-dd");

                ((Literal)(e.Row.FindControl("LtrMonth"))).Text = number.Key.ToString();

                ((Literal)(e.Row.FindControl("LtrTopic"))).Text = String.Format("{0}", number.Value[0]);

                ((Literal)(e.Row.FindControl("LtrNumber"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1"), number.Value[11]);

                ((Literal)(e.Row.FindControl("LtrProduct"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "action", NextAction.PRODUCT), number.Value[1]);

                ((Literal)(e.Row.FindControl("LtrJustdoit"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "action", NextAction.JUSTDOIT, "stat", "1"), number.Value[2]);

                ((Literal)(e.Row.FindControl("LtrCproject"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "action", NextAction.CPROJECT, "stat", "1"), number.Value[3]);

                ((Literal)(e.Row.FindControl("LtrCgroup"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "action", NextAction.GROUPC, "stat", "1"), number.Value[4]);

                ((Literal)(e.Row.FindControl("LtrPool"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "action", NextAction.TBD, "stat", "1"), number.Value[5]);

                ((Literal)(e.Row.FindControl("LtrMkt"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "action", NextAction.MKT, "stat", "1"), number.Value[6]);

                ((Literal)(e.Row.FindControl("LtrNextRound"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "action", NextAction.NEXTROUND, "stat", "1"), number.Value[7]);

                ((Literal)(e.Row.FindControl("LtrOtherbu"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "action", NextAction.OTHERBU, "stat", "1"), number.Value[8]);

                ((Literal)(e.Row.FindControl("LtrAward"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "award", "1", "stat", "1"), number.Value[9]);

                ((Literal)(e.Row.FindControl("LtrIP"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", monthF, "topicdt", monthT, "stat", "1", "ip", "1", "stat", "1"), number.Value[10]);

            }
            else if (e.Row.RowType == DataControlRowType.Footer)    // Footer
            {
                ((Literal)(e.Row.FindControl("LtrNumber"))).Text = all[11].ToString();

                ((Literal)(e.Row.FindControl("LtrProduct"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "action", NextAction.PRODUCT), all[1]);

                ((Literal)(e.Row.FindControl("LtrJustdoit"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "action", NextAction.JUSTDOIT, "stat", "1"), all[2]);

                ((Literal)(e.Row.FindControl("LtrCproject"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "action", NextAction.CPROJECT, "stat", "1"), all[3]);

                ((Literal)(e.Row.FindControl("LtrCgroup"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "action", NextAction.GROUPC, "stat", "1"), all[4]);

                ((Literal)(e.Row.FindControl("LtrPool"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "action", NextAction.TBD, "stat", "1"), all[5]);

                ((Literal)(e.Row.FindControl("LtrMkt"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "action", NextAction.MKT, "stat", "1"), all[6]);

                ((Literal)(e.Row.FindControl("LtrNextRound"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "action", NextAction.NEXTROUND, "stat", "1"), all[7]);

                ((Literal)(e.Row.FindControl("LtrOtherbu"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "action", NextAction.OTHERBU, "stat", "1"), all[8]);

                ((Literal)(e.Row.FindControl("LtrAward"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "award", "1", "stat", "1"), all[9]);

                ((Literal)(e.Row.FindControl("LtrIP"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicdf", this.from.ToString("yyyy-MM-dd"), "topicdt", this.to.ToString("yyyy-MM-dd"), "stat", "1", "ip", "1", "stat", "1"), all[10]);

            }
        }

    }
}